<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.HashMap,
                java.util.ArrayList,
                com.scalar.chiptrack.operations.process.VendorLocation,
                com.scalar.chiptrack.operations.process.dao.VendorLocationDAO,
                com.scalar.chiptrack.operations.vendor.dao.VendorDAO,
                com.scalar.chiptrack.operations.process.Transport,
                com.scalar.chiptrack.operations.process.dao.TransportDAO,
                com.scalar.chiptrack.utils.ConfigManager"%>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>
<!--chiptrack:checkLogon sourcePage="/jsp/operations/transport_locations.jsp"/-->
<chiptrack:checkLogon sourcePage="/baseProductList.do"/>
<%String chipTracContext = "/" + ConfigManager.getChipTracContext();%>
<html:html locale="true">
<HEAD><TITLE>Scalarsoft - ChipTrac</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<LINK href='<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css' type='text/css' rel='stylesheet'>
<script src="<%=chipTracContext%>/jsp/includes/scripts/functions.js"></script>
<script language='javascript'>
function saveTransportLocationsX() {
    var loclength = document.transportForm.sourceLocationID.length;
    var pa	= document.getElementById("message");
    // if(loclength != -1){ // changed by hareesh on 29th August 2003
    if (loclength == null){
        var normal_cost		= document.transportForm.normalCost.value;
		var express_cost	= document.transportForm.expressCost.value;
		var normal_time		= document.transportForm.normalTime.value;
		var express_time	= document.transportForm.expressTime.value;

        if(normal_time!=''){
			if (!isInteger(normal_time)){
				pa.innerHTML = "Please enter valid Normal Time,It must be number";
				document.transportForm.normalTime.select();
				document.transportForm.normalTime.focus();
				return;
			}
		}
        if(normal_cost!=''){
			if (!isFloat(normal_cost)){
				pa.innerHTML = "Please enter valid Normal Cost,It must be float";
				document.transportForm.normalCost.select();
				document.transportForm.normalCost.focus();
				return;
			}
		}
        if(express_time!=''){
			if (!isInteger(express_time)){
				pa.innerHTML = "Please enter valid Express Time,It must be number";
				document.transportForm.expressTime.select();
				document.transportForm.expressTime.focus();
				return;
			}
		}
        if(express_cost!=''){
			if (!isFloat(express_cost)){
				pa.innerHTML = "Please enter valid Express Cost,It must be float";
				document.transportForm.expressCost.select();
				document.transportForm.expressCost.focus();
				return;
			}
		}
   } else {
       for(var i=0;i<loclength;i++){
            var normal_cost		= document.transportForm.normalCost[i].value;
		    var express_cost	= document.transportForm.expressCost[i].value;
		    var normal_time		= document.transportForm.normalTime[i].value;
		    var express_time	= document.transportForm.expressTime[i].value;

			if(normal_time!=''){
				if (!isInteger(normal_time)){
    				pa.innerHTML = "Please enter valid Normal Time,It must be number";
	    			document.transportForm.normalTime[i].select();
		    		document.transportForm.normalTime[i].focus();
			    	return;
				}
			}
            if(normal_cost!=''){
				if (!isFloat(normal_cost)){
				    pa.innerHTML = "Please enter valid Normal Cost,It must be float";
				    document.transportForm.normalCost[i].select();
				    document.transportForm.normalCost[i].focus();
				    return;
				}
			}
			if(express_time!=''){
				if (!isInteger(express_time)){
    				pa.innerHTML = "Please enter valid Express Time,It must be number";
	    			document.transportForm.expressTime[i].select();
		    		document.transportForm.expressTime[i].focus();
			    	return;
				}
			}
	    	if(express_cost!=''){
				if (!isFloat(express_cost)){
    				pa.innerHTML = "Please enter valid Express Cost,It must be float";
	    			document.transportForm.expressCost[i].select();
		    		document.transportForm.expressCost[i].focus();
			    	return;
				}
			}
       }

    }
    document.transportForm.submit();
}
function saveTransportLocations() {
    document.transportForm.submit();
}
function back(){
    window.location = '<%=chipTracContext%>/processView.do?action=true&productID='+document.transportForm.baseProductId.value;
}
</script>
</HEAD>
<BODY bgColor='#ffffff' leftMargin='0' topMargin='0' marginheight='0' marginwidth='0'>
<!-- Top Header Start-->
<%@ include file="/jsp/includes/header.jsp" %>
<!-- Top Header End-->
<TABLE cellSpacing='0' cellPadding='0' width='100%' border='0'>
  <TBODY>
  <TR>
	<td width='180' valign='top' background='<%=chipTracContext%>/jsp/includes/images/left_red_bg.gif'>

<!-- Side Navigation start -->

<%@ include file="/jsp/operations/operations_menu.jsp" %>

<!-- end of the side navigations -->

<TD bgcolor="#ffffff">
<IMG height=9 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8></TD>
<TD bgcolor="#ffffff"><IMG height=480 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=2></TD>
<TD valign="top" width="100%">

<p align="center">

<IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/clear.gif" width=500 border=0>
<BR>

<!-- from here your body starts -->

<span class="NArialW">Transport Locations</span>

<p align="right"></p>

<html:form name="transportForm" type="com.scalar.chiptrack.ChipTrackActionForm" action="/transport.do?action=save" >

<input type="hidden" name="binSetupEnabled" value='<%=request.getAttribute ("binSetupEnabled")%>'>
<input type="hidden" name="trackId" value='<%=request.getAttribute ("trackId")%>'>
<input type="hidden" name="stepName" value='<%=request.getAttribute ("stepName")%>'>
<input type="hidden" name="previousStepName" value='<%=request.getAttribute ("previousStepName")%>'>
<input type="hidden" name="productName" value='<%=request.getAttribute ("productName")%>'>
<input type="hidden" name="baseProductId" value='<%=request.getAttribute ("baseProductId")%>'>
<input type="hidden" name="comingFrom" value='<%=request.getAttribute ("comingFrom")%>'>

 <center>
    <strong><font face="Verdana" size="-5" align="center" color="#ff0000" id="message"></font>
 </center>

<table border="0" width="98%" bordercolor="#FFFFFF" height="107">

    <tr><td colspan=7 class="NArialL" height="18">Transport Information</td></tr>

    <tr>
        <td width="24%" align="center" bgcolor="#993333" height="1">
            <font class="NArialL"><font color="#FFFFFF">Source</font>
        </td>

        <td width="24%" align="center" bgcolor="#993333" height="1">
            <font class="NArialL"><font color="#FFFFFF">Destination</font>
        </td>

        <td width="24%" align="center" bgcolor="#993333" colspan="2" height="1">&nbsp;&nbsp;&nbsp;&nbsp;
            <font class="NArialL"><font color="#FFFFFF">Normal Shipping </font><font color="#FFFFFF" class="NArialL">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <font class="NArialL"><font color="#FFFFFF">Time</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <font class="NArialL"><font color="#FFFFFF">Cost</font></font></font></font></font>
        </td>

        <td width="24%" align="center" bgcolor="#993333" colspan="2" height="1">&nbsp;&nbsp;&nbsp;&nbsp;
            <font class="NArialL"><font color="#FFFFFF">Express Shipping </font><font color="#FFFFFF" class="NArialL">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <font class="NArialL"><font color="#FFFFFF">Time</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <font class="NArialL"><font color="#FFFFFF">Cost</font></font></font></font></font>
        </td>
    </tr> 

    <%
        int processStepId = Integer.parseInt ((String)request.getAttribute ("processStepId"));          //(String) request.getParameter ("processStepId"));         //
        int previousStepId = Integer.parseInt ((String)request.getAttribute ("previousStepId"));        //(String) request.getParameter ("previousStepId"));       //

        int newProcessStepId = -1;
        try {
            newProcessStepId = Integer.parseInt ((String)request.getAttribute ("newProcessStepId"));        //(String) request.getParameter ("newProcessStepId"));       // 
        }
        catch (NumberFormatException nfe) {
            //no new processStepId is created
        }
    %>


    <%if (newProcessStepId != -1) {%>
        <input type='hidden' name='processStepId' value='<%=newProcessStepId%>'>
        <input type='hidden' name='oldProcessStepId' value='<%=processStepId%>'>
    <%} else {%>
        <input type='hidden' name='processStepId' value='<%=processStepId%>'>
        <input type='hidden' name='oldProcessStepId' value='-1'>
    <%}%>


    <%

        HashMap transportMap = (HashMap) TransportDAO.getTransportInfoMap (processStepId);
        if (transportMap == null) transportMap = new HashMap();

        ArrayList sourceLocations = VendorDAO.getSelectedLocations (previousStepId);
        ArrayList destLocations = null;

        if (newProcessStepId != -1) {
            destLocations = VendorDAO.getSelectedLocations (newProcessStepId);
        }
        else {
            destLocations = VendorDAO.getSelectedLocations (processStepId);
        }

        if (sourceLocations == null) sourceLocations = new ArrayList();
        if (destLocations == null) destLocations = new ArrayList();

        int sourceLocationsSize = sourceLocations.size();
        int destLocationsSize = destLocations.size();

        if ((sourceLocationsSize <= 0) || (destLocationsSize <= 0)) {
        // IF THERE ARE NO LOCATIONS TO STORE 
        // SEND IT DIRECTLY TO TRANSPORT_ACTION CLASS 
    %>
        <script>javascript:saveTransportLocations()</script>
    <%
        }

        if ((sourceLocationsSize == 1) && (destLocationsSize == 1) && 
            (VendorDAO.getAddressIdByLocationId(((VendorLocation) sourceLocations.get(0)).getLocationIDAsInt()) == VendorDAO.getAddressIdByLocationId(((VendorLocation) destLocations.get(0)).getLocationIDAsInt())) ) {
                // RETURN IF BOTH SOURCE AND DESTINATION ADDRESSES ARE SAME
    %>
        <script>javascript:saveTransportLocations()</script>
    <%      
        }

        VendorLocation sourceLocation = null;
        VendorLocation destLocation = null;
        String sourceLocationName = null;
        int sourceLocationID = -1;
        String destLocationName = null;
        int destLocationID = -1;

        String hashMapKey = null;
        Transport transport = null;

        for (int i=0; i<sourceLocationsSize; i++) {
            sourceLocation = (VendorLocation) sourceLocations.get(i);
            sourceLocationName = sourceLocation.getLocationName();
            if (sourceLocationName == null) sourceLocationName = "";
            sourceLocationID = sourceLocation.getLocationIDAsInt();

            for (int j=0; j<destLocationsSize; j++) {
                destLocation = (VendorLocation) destLocations.get(j);
                destLocationName = destLocation.getLocationName();
                if (destLocationName == null) destLocationName = "";
                destLocationID = destLocation.getLocationIDAsInt();
                
                if (VendorDAO.getAddressIdByLocationId(sourceLocationID) != VendorDAO.getAddressIdByLocationId(destLocationID)) {

                    hashMapKey = sourceLocationID + "|" + destLocationID;
                    transport = (Transport) transportMap.get (hashMapKey);
    %>
                <tr>
                    <td width="20%"  align="center"  bgcolor="#DEE1E2">  <font class="NArialL">  
                    <input type="text" name="sourceLocationName" value='<%=sourceLocationName%>' disabled class="txfield"/>
                    <input type="hidden" name="sourceLocationName" value='<%=sourceLocationName%>'/>
                    <input type="hidden" name="sourceLocationID" value='<%=sourceLocationID%>' size="18" class="txfield" /></font></td>

                    <td width="20%"  align="center"  bgcolor="#DEE1E2">  <font class="NArialL">  
                    <input type="text" name="destLocationName" value='<%=destLocationName%>' disabled class="txfield"/>
                    <input type="hidden" name="destLocationName" value='<%=destLocationName%>' />
                    <input type="hidden" name="destLocationID" value='<%=destLocationID%>' size="18" class="txfield" /></font></td>

                    <td width="12%"  align="center"  bgcolor="#DEE1E2">  <font class="NArialL">  <input type=text name="normalTime" value='<%if (transport != null) out.print (transport.getNormalTime());%>' size="6" class="txfield" /></font></td>
                    <td width="12%"  align="center"  bgcolor="#DEE1E2">  <font class="NArialL">  <input type=text name="normalCost" value='<%if (transport != null) out.print (transport.getNormalCost());%>' size="6" class="txfield" /></font></td>
                    <td width="12%"  align="center"  bgcolor="#DEE1E2">  <font class="NArialL">  <input type=text name="expressTime" value='<%if (transport != null) out.print (transport.getExpressTime());%>' size="6" class="txfield" /></font></td>
                    <td width="12%"  align="center"  bgcolor="#DEE1E2">  <font class="NArialL">  <input type=text name="expressCost" value='<%if (transport != null) out.print (transport.getExpressCost());%>' size="6" class="txfield" /></font></td>
                </tr>
    <%
                }
            }
        }
    %>


    <tr><td colspan=4 height="27">&nbsp;</td></tr>
    <tr align="center"><td align=center colspan=9 height="27">
        <!-- Changed by hareesh on 18th Septemeber 2003 -->
        <!--a href="javascript:saveTransportLocations()"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/next.gif" width="53" height="17"> </a-->
        <chiptrack:restrictedLink functionality='<%=Functionality.PRODUCT_SETUP%>' action='<%=User.USER_ACTION_MODIFY%>' href='javascript:saveTransportLocationsX();' onmouseover="window.status='Click here to save this Transport Locations';return true" onmouseout="window.status='';return true">
        <img border='0' src='<%=chipTracContext%>/jsp/includes/images/next.gif'></chiptrack:restrictedLink>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <!--a href="#"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/cancel.gif" width="53" height="17"></a-->
         <chiptrack:restrictedLink functionality='<%=Functionality.PRODUCT_SETUP%>' action='<%=User.USER_ACTION_MODIFY%>' href='javascript:back();' onmouseover="window.status='Click here to go back to Process View';return true" onmouseout="window.status='';return true">
        <img border='0' src='<%=chipTracContext%>/jsp/includes/images/cancel.gif'></chiptrack:restrictedLink></a>
        </td>
    </tr>

</table>


<!-- till here -->
<BR><BR><BR>
</td></tr>
<TR><TD bgcolor="#ffffff"><IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=1></TD></TR>
</TABLE>

</html:form>

<BR><BR>

<!-- Footer starts here -->

<%@ include file="/jsp/includes/footer.jsp" %>

<!-- footer end here -->

</body>

</html:html>
